package com.carrie.mdg.myviewtext.fragments;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.carrie.mdg.myviewtext.R;
import com.carrie.mdg.myviewtext.adapter.NewsAdapter;
import com.carrie.mdg.myviewtext.entity.News;

import java.util.ArrayList;
import java.util.List;

public class NewsTitleFragment extends Fragment {
    private RecyclerView recyclerView;
    private NewsAdapter adapter;
    private List<News> mNews;

    public NewsTitleFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news_title, container, false);
        initNews();
        recyclerView = (RecyclerView) view.findViewById(R.id.news_title_recycler_view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        adapter = new NewsAdapter(mNews);
        recyclerView.setAdapter(adapter);
    }

    private void initNews() {
        mNews = new ArrayList<>(20);
        News news;
        for (int i = 0; i < 20; i++) {
            news = new News();
            news.setTitle("This is news" + i);
            news.setContent("this is news this is news " + i);
            mNews.add(news);
        }
    }

}
